package com.nbufe.service.impl;

import com.nbufe.domain.vo.predict.PredictDataVo;
import com.nbufe.domain.vo.predict.TotalPredictVo;
import com.nbufe.exception.SystemException;
import com.nbufe.service.CarbonEmissionPredictService;
import com.nbufe.utils.AutoRegressionModelUtil;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author luzhiheng
 * @Date 2024/1/18 17:42
 */
@Service
public class CarbonEmissionPredictServiceImpl implements CarbonEmissionPredictService {

    @Override
    public PredictDataVo autoRegressionPrediction(Double[] data, int order, int futureYears) {
        LocalDate now = LocalDate.now();
        int year = now.getYear();

        // 示例季节性因素数据
        double[] seasonalData = {50, 80, 30, 60, 40, 70, 90, 55};

        double[] baseData = new double[data.length];
        for (int i = 0; i < data.length; i++) {
            baseData[i] = data[i];
        }
        List<TotalPredictVo> totalPredictVoList = new ArrayList<>();
        try {

            double[] forecastValues = AutoRegressionModelUtil.autoRegressionWithSeasonalityPrediction(baseData, futureYears, order, seasonalData);
            for (int i = 0; i < futureYears; i++) {
                totalPredictVoList.add(new TotalPredictVo(year + 1 + i, forecastValues[i]));
            }
        } catch (Exception e) {
            // 处理异常
            throw new SystemException("碳排放预测失败");
        }

        PredictDataVo predictDataVo = new PredictDataVo();
        predictDataVo.setTotalPredictVoList(totalPredictVoList);
        return predictDataVo;
    }
}
